package cn.pzhu.interceptor;

import cn.pzhu.model.Privilege;
import cn.pzhu.model.User;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

import java.util.List;

/**
 * Created by dongp on 2016/4/23.
 */
public class AdminLoginInterceptor implements Interceptor {

    @Override
    public void intercept(Invocation inv) {
        String actionKey = inv.getActionKey();
        Controller c = inv.getController();
        User user = c.getSessionAttr("user");
        List<Privilege> privileges = c.getSessionAttr("privileges");

        boolean canAccess = false;
        if(user != null) {
            for (Privilege p : privileges) {
                if (p.getStr("accessVal").contains(actionKey)) {
                    canAccess = true;
                    break;
                }
            }
        }
        if(canAccess) {
            inv.invoke();
        }else {
            c.render("login.jsp");
        }
    }
}
